/**
 * 
 */
package org.t2.game.server.module.server.api;

import java.time.LocalDateTime;

import org.gaming.service.advice.ResponseResult;
import org.gaming.service.asserts.Asserts;
import org.gaming.tool.GsonUtil;
import org.gaming.tool.LocalDateTimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.t2.design.module.CheckTable;
import org.t2.design.module.autogen.CheckAbstractTable.CheckCfg;
import org.t2.game.server.foundation.player.LogoutInternalMessage;
import org.t2.game.server.foundation.player.PlayerActorPool;
import org.t2.game.server.foundation.player.PlayerContext;
import org.t2.game.server.foundation.player.PlayerContextManager;
import org.t2.game.server.module.server.model.ServerInfo;
import org.t2.game.server.module.server.service.ServerInfoService;
import org.t2.game.server.module.tips.TipsCode;

import pb.t2.protobuf.player.Player.PbLogoutType;
import sh.t2.share.apiutil.GameApi;
import sh.t2.share.module.mail.MailAttachment;
import sh.t2.share.module.server.form.ServerApiProtocol.ServerCheckRewardsRequest;
import sh.t2.share.module.server.form.ServerApiProtocol.ServerCheckRewardsResponse;
import sh.t2.share.module.server.form.ServerApiProtocol.ServerMaintainRequest;
import sh.t2.share.module.server.form.ServerApiProtocol.ServerMaintainResponse;
import sh.t2.share.module.server.form.ServerApiProtocol.ServerSetOpenTimeRequest;
import sh.t2.share.module.server.form.ServerApiProtocol.ServerSetOpenTimeResponse;

/**
 * @author YY
 *
 */
@Controller
public class ServerApi {

	private static Logger logger = LoggerFactory.getLogger(ServerApi.class);
	
	@Autowired
	private ServerInfoService serverInfoService;
	@Autowired
	private PlayerContextManager playerContextManager;
	
	//忽略预期开服时间，直接开服，以当前时间为实际开服时间
	@RequestMapping(value = GameApi.serverOpenNow)
	@ResponseBody
	public ResponseResult<String> openNow() {
		logger.info("收到服务器立刻开服通知");
		
		ServerInfo serverInfo = serverInfoService.getMain();
		if(serverInfo.getOpenTime().isBefore(LocalDateTime.now()) || serverInfo.isOpened()) {
			logger.error("当前服务器已经开服");
			Asserts.isTrue(false, TipsCode.SERVER_OPENED);
		}
		serverInfo.setOpenTime(LocalDateTime.now());
		serverInfoService.update(serverInfo);
		
		return new ResponseResult<>();
	}
	
	//设置预期开服时间
	@RequestMapping(value = GameApi.serverSetOpenTime)
	@ResponseBody
	public ServerSetOpenTimeResponse setOpenTime(@RequestBody ServerSetOpenTimeRequest request) {
		logger.info("收到服务器设置开服时间通知{}", GsonUtil.toJson(request));
		
		ServerInfo serverInfo = serverInfoService.getMain();
		if(serverInfo.getOpenTime().isBefore(LocalDateTime.now()) || serverInfo.isOpened()) {
			logger.error("当前服务器已经开服");
			Asserts.isTrue(false, TipsCode.SERVER_OPENED);
		}
		serverInfo.setOpenTime(LocalDateTimeUtil.ofEpochMilli(request.openTime));
		serverInfoService.update(serverInfo);
		return new ServerSetOpenTimeResponse();
	}
	
	//服务器维护
	@RequestMapping(value = GameApi.serverMaintain)
	@ResponseBody
	public ServerMaintainResponse maintain(@RequestBody ServerMaintainRequest request) {
		ServerInfo serverInfo = serverInfoService.getMain();
		serverInfo.setMaintain(request.maintain);
		serverInfoService.update(serverInfo);
		
		if(request.maintain) {
			for(PlayerContext playerContext : playerContextManager.onlines()) {
				LogoutInternalMessage message = new LogoutInternalMessage();
				message.playerContext = playerContext;
				message.logoutType = PbLogoutType.SERVER_MAINTAIN;
				PlayerActorPool.tell(message);
			}
		}
		logger.info("收到服务器维护通知{}", request.maintain);
		return new ServerMaintainResponse();
	}
	
	/**
	 * 提供给后台请求当前服务器是否可用的接口
	 * @return
	 */
	@RequestMapping(value = GameApi.serverTestRunning)
	@ResponseBody
	public ResponseResult<String> testRunning() {
		return new ResponseResult<>();
	}
	
	
	@RequestMapping(value = GameApi.serverCheckRewards)
	@ResponseBody
	public ServerCheckRewardsResponse checkRewards(@RequestBody ServerCheckRewardsRequest request) {
		ServerCheckRewardsResponse response = new ServerCheckRewardsResponse();
		for(MailAttachment reward : request.rewards) {
			CheckCfg checkCfg = CheckTable.ins().findInIdIndex(reward.getItem());
			Asserts.isTrue(checkCfg != null, TipsCode.DESIGN_NOT_FOUND);
			
			MailAttachment newReward = new MailAttachment();
			newReward.setItem(checkCfg.getId());
			newReward.setName(checkCfg.getName());
			newReward.setNum(reward.getNum());
			response.rewards.add(newReward);
		}
		return response;
	}
	
}
